// This is the main DLL file.

#include "stdafx.h"
#include "RApi.h"
#include "ConnectionSetup.h"

using namespace System::IO;
using namespace System::Globalization;

namespace Tradex {
	namespace Connectivity {
		namespace Rithmic {

			ConnectionSetup^ ConnectionSetup::Load (String^ directory, String^ configuration) {
				XmlSerializer^ pXmlSerializer = gcnew XmlSerializer (ConnectionSetup::typeid);
				String^ pPath = Path::Combine (directory, configuration + ".xml");
				FileStream^ pFs = gcnew FileStream(pPath, FileMode::Open);
				try {
					ConnectionSetup^ pRetval = (ConnectionSetup^) pXmlSerializer->Deserialize(pFs);
					return pRetval;
				} finally {
					pFs->Close ();
				}
			};

			char** ConnectionSetup::CreateEnvironment (String^ directory, String^ user) {
				int envs = Environment->Length;
				char** pEnv = new char* [envs+1];
				for (int i= 0; i <= envs; i++) {
					if (i == envs) {
						pEnv[i] = NULL;
						break;
					}
					// We preformat some strings. Sadly necessary - we need to have the user name and directory somewhere in here.
					String^ ev = String::Format(CultureInfo::InvariantCulture, Environment[i], directory, user);
					pEnv[i] = (char*)(void*) Marshal::StringToHGlobalAnsi(ev);
				}
				return pEnv;
			};

			void ConnectionSetup::DeleteEnvironment (char* environment []) {
				int envs = Environment->Length;
				for (int i= 0; i < envs; i++) {
					Marshal::FreeHGlobal(IntPtr(environment[i]));
				}
				delete environment;
			};

		}
	}
}
